"""
宁波大学-书生镜像 图书封面拷贝
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.getcwd()))
import shutil
import utils


def copy_cover():

    srcroot = r'\\192.168.0.74\样例数据\sursen\shushengbook_cover_20160729'
    dstroot = r'E:\venter\r2d2\download\nbdx_shushengbook\shusheng_cover'
    conn = utils.init_db('mysql', 'nbu')
    cur = conn.cursor()
    cur.execute("select id,bookid from shusheng")
    rows = cur.fetchall()
    cnt = 0
    rows_dic = {str(item[0]): item[0] for item in rows}
    for file, fullpath in utils.file_list(srcroot):
        basename, _, ext = file.partition('.')
        cnt += 1
        if cnt % 1000 == 1:
            print(cnt)
        if not rows_dic.get(basename):
            continue
        dst_dir = os.path.join(dstroot, file[:2])
        if not os.path.exists(dst_dir):
            os.makedirs(dst_dir)
        dst_file = os.path.join(dst_dir, file)
        shutil.copyfile(fullpath, dst_file)
        cur.execute("update shusheng set cover_stat=1 where bookid={}".format(basename))
        conn.commit()
    conn.close()


def diff_cover():
    cover_path = r'E:\vendetter\project\python\work\download\ldgx_shusheng\cover\20171103'
    total = set()
    partion = set()
    for file, fullpath in utils.file_list(cover_path):
        id_, _, _ = file.partition('.')
        total.add(id_)
    with open('./cover.txt', encoding='utf8') as f:
        for line in f:
            partion.add(line.strip())
    union = total - partion
    print(union)


if __name__ == '__main__':
    copy_cover()